package com.dabaozhan.modules.wx_app.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.dabaozhan.common.exception.RRException;
import com.dabaozhan.common.utils.ValidationBeanUtils;
import com.dabaozhan.modules.wx_app.base.ResponseDto;
import com.dabaozhan.modules.wx_app.entity.SiteOrderEntity;
import com.dabaozhan.modules.wx_app.enums.ResponseCodeEnum;
import com.dabaozhan.modules.wx_app.facade.SiteOrderFacade;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 手工录入模块
 */
@Api(value = "手工录入模块")
@RestController
@RequestMapping("/wx_app")
public class SiteOrderController {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(SiteOrderController.class);
	
	@Autowired
	private SiteOrderFacade siteOrderFacade;

	@ApiOperation(value = "手工录入供应商订单")
    @RequestMapping(value = "/addSiteOrder")
	public String addSiteOrder(SiteOrderEntity entity,HttpServletRequest request){
		ResponseDto<SiteOrderEntity> responseDto = new ResponseDto<SiteOrderEntity>();
		try{
			//验证参数是否为空
			ValidationBeanUtils.validateBeanRequestDto(entity);
			//图片保存路径
			String pathUrl = request.getSession().getServletContext().getRealPath("/")+ ("fileImage/");
			SiteOrderEntity responseEntity = siteOrderFacade.createSiteOrder(entity,pathUrl);
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
			responseDto.setResult(responseEntity);
		}catch(RRException re){
			responseDto.setCode(re.getCode());
			responseDto.setMessage(re.getMessage());
		}catch (Exception e) {
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
			//记录异常日志
			LOGGER.error("手工录入供应商订单发生异常,异常详情如下:",e);
		}finally{
			//记录请求入参和返回日志
			LOGGER.info("手工录入供应商订单请求参数:{}-返回结果:{}",JSON.toJSONString(entity),JSON.toJSONString(responseDto));
		}
		return JSON.toJSONString(responseDto);
	}

	@ApiOperation(value = "供应商订单结算(查询手工录入的订单信息")
    @RequestMapping(value = "/settlementOrder")
	public String settlementOrder(String orderNumber){
		ResponseDto<SiteOrderEntity> responseDto = new ResponseDto<SiteOrderEntity>();
		try{
			SiteOrderEntity response =  siteOrderFacade.getSiteOrderByOrderNumber(orderNumber);
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
			responseDto.setResult(response);
		}catch(RRException re){
			responseDto.setCode(re.getCode());
			responseDto.setMessage(re.getMessage());
		}catch (Exception e) {
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
			//记录异常日志
			LOGGER.error("供应商订单结算(查询手工录入的订单信息)异常,异常详情如下:",e);
		}finally{
			//记录请求入参和返回日志
			LOGGER.info("供应商订单结算(查询手工录入的订单信息)请求参数:{}-返回结果:{}","orderNumber:"+orderNumber,JSON.toJSONString(responseDto));
		}
		return JSON.toJSONString(responseDto);
	}
}
